<?php

namespace MoChat\App\Sales\Command;

use Hyperf\Command\Command as HyperfCommand;
use Hyperf\DbConnection\Db;
use Hyperf\Redis\Redis;
use Hyperf\Utils\ApplicationContext;
use Psr\Container\ContainerInterface;
use Symfony\Component\Console\Input\InputArgument;
use TheSeer\Tokenizer\Exception;
use Hyperf\Command\Annotation\Command;

/**
 * @Command
 */
class B2bClientCommand extends HyperfCommand
{
    /**
     * @var ContainerInterface
     */
    protected $container;

    public function __construct(ContainerInterface $container)
    {
        $this->container = $container;

        parent::__construct('b2bClient');
    }

    public function configure()
    {
        parent::configure();
        $this->addOption('clear', 'C', InputArgument::OPTIONAL, '清除redis', 0);
        $this->setDescription('客户内码同步');
    }

    public function handle()
    {
        $container = ApplicationContext::getContainer();
        $redis = $container->get(Redis::class);
        $clear = $this->input->getOption('clear');
        if ($clear) {
            $redis->del('B2bClientCommand_key');
        }
        $bool = $redis->get('B2bClientCommand_key');
        if ($bool) {
            $this->line('上一个销售任务同步脚本程序正在执行中', 'info');
        } else {
            $redis->set('B2bClientCommand_key', true, 300);
            $maxId = $redis->get('B2bClientCommand_key_max_id');
            $maxId = $maxId ?: 0;
            Db::beginTransaction();
            try {
                $client = Db::table('client')
                    ->whereRaw('unique_id is null or unique_id=""')
                    ->limit(1000)
                    ->where('id','>',$maxId)
                    ->orderBy('id', 'asc')
                    ->pluck('enterprise_name', 'id');
                $client = empty($client) ? [] : $client->toArray();
                if (!empty($client)) {
                    $users = Db::connection('b2b')->table('user')
                        ->whereIn('business_name', $client)
                        ->select([
                            'unique_id',
                            'business_name',
                            'status',
                            'review',
                        ])
                        ->pluck('unique_id', 'business_name');
                    $users = empty($users) ? [] : array_column($users->toArray(),null,'business_name');
                    foreach ($client as $id=>$name){
                        Db::table('client')
                            ->where('id', $id)
                            ->update([
                                'unique_id' => $users[$name]['unique_id'] ?? 0,
                                'status' => $users[$name]['status'] ?? 0,
                                'review' => $users[$name]['review'] ?? 0,
                            ]);
                    }
                }
                $maxId = empty($client) ? 0 : max(array_flip($client));
                $redis->set('B2bClientCommand_key_max_id', $maxId);
                Db::commit();
            } catch (\Throwable $throwable) {
                Db::rollBack();
            }
            $redis->del('B2bClientCommand_key');
            $this->line('客户内码同步处理完成', 'info');
        }
    }
}
